package leetcode.code019.RemoveNthNodeFromEndofList;

import leetcode.helper.tree.ListNode;

/**
 * 一次 + 双针
 *
 * @author Max
 * @date 2021年8月16日
 * @description
 */
public class Solution4 extends RemoveNthNodeFromEndofList {

	public static void main(String[] args) {
		RemoveNthNodeFromEndofList so = new Solution4();
		so.debug1();
		so.debug2();
		so.debug3();
//		so.debug4();
	}

	@Override
	public ListNode removeNthFromEnd(ListNode head, int n) {
		ListNode fast = head;
		ListNode slow = head;
		while (fast != null) {
			fast = fast.next;
			if (n == -1) // 数数吧
				slow = slow.next;
			else
				n--;
		}
		if (n == 0)
			return head.next;
		else
			slow.next = slow.next.next;
		return head;
	}

}
